Beginners Programming Series IX

Appendix A


The Calculator Code



'LB Calculator - by Brad Moore
'Copyright 2004
'------------------------------------------------------------------------
'This source release as free-software.  It is not in the public domain,
'however you may use it, change it, sell it, or re-release it 
'without obtaining permission from the author.  You may not transfer these
'rights.  This notice must be somewhere in you code and/or documenation.
'Please credit the original author in derived works.
'------------------------------------------------------------------------
'Form created with the help of Freeform 3 v03-27-03
'Generated on Aug 27, 2004 at 20:19:01


buffer$ = ""
currentop$ = ""
lastop$ = ""
eval = 0


[setup.main.Window]

    '-----Begin code for #main

    nomainwin
    WindowWidth = 415
    WindowHeight = 330
    UpperLeftX=int((DisplayWidth-WindowWidth)/2)
    UpperLeftY=int((DisplayHeight-WindowHeight)/2)


    '-----Begin GUI objects code
    
    TextboxColor$ = "white"
    stylebits #main.tbx, _ES_RIGHT, 0, 0, 0
    textbox #main.tbx,  20,   7, 365,  35
    button #main.btn9,"9",[9buttonClick], UL, 170,  87,  65,  40
    button #main.btn8,"8",[8buttonClick], UL,  95,  87,  65,  40
    button #main.btn7,"7",[7buttonClick], UL,  20,  87,  65,  40
    button #main.btn6,"6",[6buttonClick], UL, 170, 137,  65,  40
    button #main.btn5,"5",[5buttonClick], UL,  95, 137,  65,  40
    button #main.btn4,"4",[4buttonClick], UL,  20, 137,  65,  40
    button #main.btn3,"3",[3buttonClick], UL, 170, 187,  65,  40
    button #main.btn2,"2",[2buttonClick], UL,  95, 187,  65,  40
    button #main.btn1,"1",[1buttonClick], UL,  20, 187,  65,  40
    button #main.btn0,"0",[0buttonClick], UL,  20, 237, 140,  40
    button #main.btndec,".",[decbuttonClick], UL, 170, 237,  65,  40
    button #main.btnquit,"Quit",[qutibuttonClick], UL,  20,  52,  65,  25
    button #main.btnc,"C",[cbuttonClick], UL,  95,  52, 140,  25
    button #main.btnplus,"+",[plusbuttonClick], UL, 245,  87,  65,  40
    button #main.btnmin,"-",[minbuttonClick], UL, 245, 137,  65,  40
    button #main.btnx,"x",[xbuttonClick], UL, 245, 187,  65,  40
    button #main.btndiv,"/",[divbuttonClick], UL, 245, 237,  65,  40
    button #main.btneq,"=",[eqbuttonClick], UL, 320,  87,  65, 190
    button #main.btnce,"CE",[cebuttonClick], UL, 245,  52, 140,  25

    '-----End GUI objects code

    open "LB Calculate" for window as #main
    print #main, "font ms_sans_serif 10"
    print #main.tbx, "!font arial_black 14"
    print #main, "trapclose [quit.main]"

    #main.tbx "0"

[main.inputLoop]   'wait here for input event
    wait



[9buttonClick]   'Perform action for the button named 'btn9'

    buffer$ = buffer$ + "9"
    #main.tbx buffer$
    wait


[8buttonClick]   'Perform action for the button named 'btn8'

    buffer$ = buffer$ + "8"
    #main.tbx buffer$
    wait


[7buttonClick]   'Perform action for the button named 'btn7'

    buffer$ = buffer$ + "7"
    #main.tbx buffer$
    wait


[6buttonClick]   'Perform action for the button named 'btn6'

    buffer$ = buffer$ + "6"
    #main.tbx buffer$
    wait


[5buttonClick]   'Perform action for the button named 'btn5'

    buffer$ = buffer$ + "5"
    #main.tbx buffer$
    wait


[4buttonClick]   'Perform action for the button named 'btn4'

    buffer$ = buffer$ + "4"
    #main.tbx buffer$
    wait


[3buttonClick]   'Perform action for the button named 'btn3'

    buffer$ = buffer$ + "3"
    #main.tbx buffer$
    wait


[2buttonClick]   'Perform action for the button named 'btn2'

    buffer$ = buffer$ + "2"
    #main.tbx buffer$
    wait


[1buttonClick]   'Perform action for the button named 'btn1'

    buffer$ = buffer$ + "1"
    #main.tbx buffer$
    wait


[0buttonClick]   'Perform action for the button named 'btn0'

    'if buffer$ length = 0 don't add zeros
    if len(buffer$) = 0 then wait
    buffer$ = buffer$ + "0"
    #main.tbx buffer$
    wait


[decbuttonClick]   'Perform action for the button named 'btndec'

    'if buffer$ already has a decimal, ignor this keypress
    if instr(buffer$,".") > 0 then wait
    buffer$ = buffer$ + "."
    #main.tbx buffer$
    wait


[qutibuttonClick]   'Perform action for the button named 'btnquit'

    goto [main.quit]


[cbuttonClick]   'Perform action for the button named 'btnc'

    'C button clears everything and starts at the beginning
    buffer$ = ""
    lastop$ = ""
    currentop$ = ""
    eval = 0
    #main.tbx "0"
    wait


[plusbuttonClick]   'Perform action for the button named 'btnplus'

    'add buffer$ to eval
    currentop$ = "+"
    goto [operation]


[minbuttonClick]   'Perform action for the button named 'btnmin'

    'subtract buffer$ to eval
    currentop$ = "-"
    goto [operation]


[xbuttonClick]   'Perform action for the button named 'btnx'

    'multiply buffer$ to eval
    currentop$ = "*"
    goto [operation]


[divbuttonClick]   'Perform action for the button named 'btndiv'

    'divide buffer$ to eval
    currentop$ = "/"
    goto [operation]


[eqbuttonClick]   'Perform action for the button named 'btneq'

    'Insert your own code here
    currentop$ = "="
    goto [operation]


[cebuttonClick]   'Perform action for the button named 'btnce'

    'Here we reset the buffer to the previous eval value
    buffer$ = str$(eval)
    if val(buffer$) = 0 then
        buffer$ = ""
        #main.tbx "0"
        wait
    else
        #main.tbx buffer$
        wait
    end if
    wait
    

[quit.main] 'End the program
    close #main
    end
    
    
[operation]
    'this routine will execute any buffered operation
    if lastop$ = "" then
        lastop$ = currentop$
        'sometimes people press an operation when a value is displayed after
        'the equals key - buffer$ is null then - check for a value displayed
        if buffer$ = "" then #main.tbx "!contents? buffer$"
        eval = val(buffer$)
        buffer$ = ""
        wait
    else
        if lastop$ = "-" then
            eval = eval - val(buffer$)
        end if
        if lastop$ = "+" then
            eval = eval + val(buffer$)
        end if
        if lastop$ = "/" then
            'protect against divide by zero
            if val(buffer$) = 0 then wait
            eval = eval / val(buffer$)
        end if
        if lastop$ = "*" then
            eval = eval * val(buffer$)
        end if
    end if
          
    if currentop$ = "=" then
        lastop$ = ""
        currentop$ = ""
        buffer$ = ""
        #main.tbx str$(eval)
        eval = 0        
    else
        lastop$ = currentop$
        buffer$ = ""
        #main.tbx str$(eval)
    end if
    wait